package com.example.demo.controller;

import com.example.demo.common.AppVariable;
import com.example.demo.common.PasswordUtil;
import com.example.demo.common.ResultAjax;
import com.example.demo.common.SessionUtil;
import com.example.demo.config.ThreadPoolConfig;
import com.example.demo.model.UserInfo;
import com.example.demo.model.vo.UserInfoVo;
import com.example.demo.service.ArticleService;
import com.example.demo.service.UserService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private ArticleService articleService;


    /**
     * 用户注册操作
     * @param userInfo
     * @return
     */
    @RequestMapping("/register")
    public ResultAjax register(UserInfo userInfo) {
        // 参数校验
        if (userInfo == null || !StringUtils.hasLength(userInfo.getUsername()) ||
                !StringUtils.hasLength(userInfo.getPassword())) {
            return ResultAjax.fail(-1,"非法参数");
        }
        // 将数据添加到数据库，加密数据
        String password = PasswordUtil.encrypt(userInfo.getPassword());
        userInfo.setPassword(password);
        int result = userService.register(userInfo);
        return ResultAjax.success(result);
    }

    /**
     * 用户登录操作
     * @return
     */
    @RequestMapping("/login")
    // 使用子类方便以后扩展功能
    public ResultAjax login(UserInfoVo userInfoVo, HttpServletRequest request) {
        // 1.参数校验
        if(userInfoVo == null || !StringUtils.hasLength(userInfoVo.getUsername()) ||
                !StringUtils.hasLength(userInfoVo.getPassword())) {
            return ResultAjax.fail(-1,"非法参数");
        }
        // 2.将用户传递的用户名与数据库数据进行比对
        UserInfo userInfo = userService.getUserByName(userInfoVo.getUsername());
        if (userInfo == null || userInfo.getId()==0) {
            // 查询不到用户
            return ResultAjax.fail(-2,"用户名或密码错误");
        }
        // 3.比对密码 解密比对
        if (!PasswordUtil.decrypt(userInfoVo.getPassword(),userInfo.getPassword())) {
            return ResultAjax.fail(-2,"用户名或密码错误");
        }
        // 4.设置session
        HttpSession session = request.getSession(true);
        session.setAttribute(AppVariable.SESSION_USER_KEY,userInfo);
        // 5.返回结果给前端 不能返回查询结果 这里利用保底策略返回一个1即可
        return ResultAjax.success(1);
    }

    /**
     * 注销操作
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public ResultAjax logout(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute(AppVariable.SESSION_USER_KEY) != null) {
            // 存在session就清除掉
            session.removeAttribute(AppVariable.SESSION_USER_KEY);
        }
        return ResultAjax.success(1);
    }

    /**
     * 文章删除
     * @param aid
     * @param request
     * @return
     */
    @RequestMapping("/delete")
    public ResultAjax delete(Integer aid, HttpServletRequest request) {
        // 参数校验
        if (aid == null || aid <= 0) {
            return ResultAjax.fail(-1,"非法参数");
        }
        // 归属人判断
        UserInfo userInfo = SessionUtil.getUser(request);
        if (userInfo == null) {
            return ResultAjax.fail(-2,"请先登录！");
        }
        // 删除
        int result = articleService.delete(aid,userInfo.getId());
        return ResultAjax.success(result);
    }
}












